﻿using Futajma.MigrationStudio.DataDefinition;
using Futajma.MigrationStudio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LLTreeManagementApplication.Utils
{
    class TreeImageUtils
    {
        static int PLUS_OFFSET = 8;
        static int ALERT_OFFSET = 16;


        internal static ImageList GetTreeImageList()
        {
            ImageList imageList = new ImageList();
            imageList.Images.Add(TypeIcons.sharepoint);
            imageList.Images.Add(TypeIcons.SiteCollection);
            imageList.Images.Add(TypeIcons.SubSite);
            imageList.Images.Add(TypeIcons.Library);
            imageList.Images.Add(TypeIcons.SPFolder);
            imageList.Images.Add(TypeIcons.Document);
            imageList.Images.Add(TypeIcons.form);
            imageList.Images.Add(TypeIcons.DeleteFolderHS);
            imageList.Images.Add(TypeIcons.sharepoint);
            imageList.Images.Add(TypeIcons.SiteCollection_Plus);
            imageList.Images.Add(TypeIcons.SubSite_Plus);
            imageList.Images.Add(TypeIcons.Library_Plus);
            imageList.Images.Add(TypeIcons.SPFolder_Plus);
            imageList.Images.Add(TypeIcons.Document_Plus);
            imageList.Images.Add(TypeIcons.Form_Plus);
            imageList.Images.Add(TypeIcons.DeleteFolderHS_Plus);
            imageList.Images.Add(TypeIcons.sharepoint);
            imageList.Images.Add(TypeIcons.SiteCollection_Alert);
            imageList.Images.Add(TypeIcons.SubSite_Alert);
            imageList.Images.Add(TypeIcons.Library_Alert);
            imageList.Images.Add(TypeIcons.SPFolder_Alert);
            imageList.Images.Add(TypeIcons.Document_Alert);
            imageList.Images.Add(TypeIcons.Form_Alert);
            imageList.Images.Add(TypeIcons.DeleteFolderHS_Alert);

            return imageList;
        }

        internal static int CalculateImageIndex(TreeNode node, int currentProjectVersion)
        {
            int offset = 0;
            DataNode data = node.Tag as DataNode;

            if (data != null)
            {
                // new inserted item
                if (data.CommitedItem == true)                
                {
                    if (data.Version == currentProjectVersion)
                    {
                        offset = (node.SelectedImageIndex % PLUS_OFFSET) + ALERT_OFFSET;
                    }
                    else
                    {
                        offset = node.SelectedImageIndex % PLUS_OFFSET;
                    }
                }
                else
                {
                    offset = (node.SelectedImageIndex % PLUS_OFFSET) + PLUS_OFFSET;
                }
                return offset;
            }
            else
            {
                return node.SelectedImageIndex;
            }
        }

        //internal static int CalculateImageIndex(TreeNode node, int currentProjectVersion)
        //{
        //    return CalculateSelectedImageIndex(node, currentProjectVersion);
        //}

        internal static int GetIconIndex(SharePointType sharePointType, int offset)
        {
            int value = 0;

            switch (sharePointType)
            {
                case SharePointType.Application:
                case SharePointType.NotDefined:
                    value = 0;
                    break;
                case SharePointType.SiteCollection:
                    value = 1;
                    break;
                case SharePointType.Site:
                    value = 2;
                    break;
                case SharePointType.DocLibrary:
                    value = 3;
                    break;
                case SharePointType.Folder:
                    value = 4;
                    break;
                case SharePointType.Item:
                    value = 6;
                    break;
                case SharePointType.Document:
                    value = 5;
                    break;
            }
            return value + offset;
        }

        internal static int GetIconIndex(SharePointType sharePointType)
        {
            return GetIconIndex(sharePointType, 0);
        }

        internal static int GetAddIconIndex(SharePointType sharePointType)
        {
            return GetIconIndex(sharePointType, PLUS_OFFSET);
        }

        internal static void SetIconIndex(TreeView tree, int currentProjectVersion)
        {
            foreach (TreeNode node in tree.Nodes)
            {
                SetIconIndex(node, currentProjectVersion);
            }
        }

        internal static void SetIconIndex(TreeNode node, int currentProjectVersion)
        {
            //Console.WriteLine(string.Format("start CalculateImageIndex[{0}]", DateTime.Now));
            node.SelectedImageIndex = CalculateImageIndex(node, currentProjectVersion);
            //Console.WriteLine(string.Format("end and start CalculateImageIndex[{0}]", DateTime.Now));
            node.ImageIndex = CalculateImageIndex(node, currentProjectVersion);
            //Console.WriteLine(string.Format("end CalculateImageIndex[{0}]", DateTime.Now));

            foreach (TreeNode child in node.Nodes)
            {
                SetIconIndex(child, currentProjectVersion);
                //SetIconIndex(child, currentProjectVersion);
            }
        }
    }
}
